var checkoutForm;

function runFormValidation() {
    checkoutForm = {
        // Rules for form validation
        rules: {
          name: {
              required: true
          },
          company: {
              required: true,
          },
          email: {
              required: true,
              email: true
          },
          truck_quantity: {
              required: true
          }
        },
        // Messages for form validation
        messages: {
            name: {
                required: "请输入姓名",
            },
            company: {
                required: "请输入公司名称"
            },
            email: {
                required: "请输入邮箱地址",
                email: "邮箱格式不正确"
            },
            truck_quantity: {
                required: "请输入车辆数"
            }
        },
        // Do not change code below
        errorPlacement: function(error, element) {
            error.insertAfter(element.parent());
        }
    };
}
var res = {
   getUrlParams: function(purl,isNoToLower) {
       isNoToLower = isNoToLower || false;
          var url = purl || window.location.href;
          var paraObj = {};
          if(url.indexOf("?")<0){
           return paraObj;
          }
     url = url.replace('#','');
          var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
          if(!isNoToLower){
           for (var i=0; j=paraString[i]; i++){
                  paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
              }
          }else{
           for (var i=0; j=paraString[i]; i++){
             try{
               paraObj[j.substring(0,j.indexOf("="))] = decodeURIComponent(j.substring(j.indexOf("=")+1,j.length));
             }catch(ex){
               paraObj[j.substring(0,j.indexOf("="))] = unescape(j.substring(j.indexOf("=")+1,j.length));
             }
              }
          }
          return paraObj;
      }
};

var res = res.getUrlParams();
$(function(){
    $("#back").click(function(){
    	history.back();
    });
    runFormValidation();
    $("#eco_info_submit").click(function(){
    	var name = $("#name").val();
    	var company = $("#company").val();
    	var email = $("#email").val();
    	//js页面验证
      if (!$('#eco_form').validate(checkoutForm).form()) {
          return false;
      }
      $("#eco_info_submit").attr("disabled",true);
      $("#eco_info_submit").text("正在提交");
  		$.ajax({
          type:"post",
          url:"../php/insertSubmitInfo.php",
          data:{"name":name, "company":company, "email":email},
          success: function(data){
              $("#eco_info_submit").removeAttr("disabled");
              $("#eco_info_submit").text("提交信息");
              if(data == '2'){
                $.jGrowl("时间超过5分钟，请访问首页重新查询", { life: 3000 });
              }else{
                $.jGrowl("信息提交成功", { life: 3000 });
              }
         }
      });
    	
    });

    $("#info_submit").click(function(){
      var name = $("#name").val();
      var company = $("#company").val();
      var truck_quantity = $("#truck_quantity").val();
      //js页面验证
      if (!$('#eff_form').validate(checkoutForm).form()) {
          return false;
      }
      $("#info_submit").attr("disabled",true);
      $("#info_submit").text("正在提交");
      $.ajax({
          type:"post",
          url:"../php/insertEffiSubmitInfo.php",
          data:{"name":name, "company":company, "truck_quantity":truck_quantity},
          success: function(data){
              $("#info_submit").removeAttr("disabled");
              $("#info_submit").text("提交信息");
              if(data == '2'){
                $.jGrowl("时间超过5分钟，请访问首页重新查询", { life: 3000 });
              }else if(data != '0'){
                $.jGrowl("信息提交成功", { life: 3000 });
              }else{
                $.jGrowl("信息提交失败", { life: 3000 });
              }
         }
      });
    });
    $("#name").focus(function(){
      $("#warnMessage").html("&nbsp;");
    });
    $("#company").focus(function(){
      $("#warnMessage").html("&nbsp;");
    });
    $("#email").focus(function(){
      $("#warnMessage").html("&nbsp;");
    });
    setEconomicValue();
});

function setEconomicValue(){
	if(res != null){
		$("#line").html(decodeURIComponent(res.line));
	    $("#oilRef").html(decodeURIComponent(res.fuel_interval));
	    $("#vehicleMol").html(decodeURIComponent(res.carstyle));
	    $("#oilResult").html(decodeURIComponent(res.sup_inf));
	}
}